﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using ConnectionLayer;
using System.Windows.Forms;

namespace BusinessLayer
{
    public static class ObraActions
    {
        public static IEnumerable<string> ListarObrasPorConcluir()
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "select * from dbo.ListarObrasNaoTerminadas";
                SqlDataReader red = cmd.ExecuteReader();
                List<String> list = new List<String>();
                while (red.Read())
                {
                    String send = red.GetValue(0).ToString() + " " + red.GetValue(1).ToString() + " " + red.GetValue(2).ToString() + " " + red.GetValue(3).ToString() + " " + red.GetValue(4).ToString() + " " + red.GetValue(5).ToString() + " " + red.GetValue(6).ToString();
                    list.Add(send);
                }
                return list;

            }
            catch (Exception e) { MessageBox.Show(e.Message); return null; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }
        }
        public static bool CriarObra(String oficina, String veiculo, String Cliente, String EstadoObra, DateTime DataRegisto, String ValorEst, String HorasEst)
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "RegistoObra";
                cmd.Parameters.AddWithValue("@idOficina", oficina);
                cmd.Parameters.AddWithValue("@dataRegisto", DataRegisto);
                cmd.Parameters.AddWithValue("@estado", EstadoObra);
                cmd.Parameters.AddWithValue("@valorEstimado", ValorEst);
                cmd.Parameters.AddWithValue("@totalHoras", HorasEst);
                cmd.Parameters.AddWithValue("@Veiculo", veiculo);
                cmd.Parameters.AddWithValue("@idCliente", Cliente);
                cmd.ExecuteScalar();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (Exception e) { MessageBox.Show(e.Message); }
            }
        }

        public static bool AlteraObra(String idObra, String estadoO, DateTime dateInit, String oficina) { 
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "update Obra set Obra.dataInicioO = @dateInit,Obra.estadoO = @estadoO where Obra.estadoO = @idObra and Obra.oficina = @oficina";
                cmd.Parameters.AddWithValue("@idObra", idObra);
                cmd.Parameters.AddWithValue("@estadoO", estadoO);
                cmd.Parameters.AddWithValue("@dateInit", dateInit);
                cmd.Parameters.AddWithValue("@oficina", oficina);
                cmd.ExecuteNonQuery();
                return true;

            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (Exception e) { MessageBox.Show(e.Message); }
            }
        }
    }
}
